package com.imy.common.base.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseSimpleListAdapter<T, VH extends BaseViewHolder> extends BaseAdapter {
    protected String TAG = this.getClass().getSimpleName();
    protected Context mContext;
    protected LayoutInflater mInflater = null;
    protected List<T> mInfos = new ArrayList<>();
    protected VH holder;

    public BaseSimpleListAdapter(Context context, List<T> infos) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        if (mInfos == null) {
            mInfos = new ArrayList<>();
        }
        mInfos.addAll(infos);
    }


    @Override
    public int getCount() {
        return mInfos.size();
    }

    @Override
    public T getItem(int position) {
        return mInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void clear() {
        mInfos.clear();
        notifyDataSetChanged();
    }

    public void addLists(List<T> list) {
        mInfos.addAll(list);
        notifyDataSetChanged();
    }

    public void clearNAddLists(List<T> list) {
        mInfos.clear();
        mInfos.addAll(list);
        notifyDataSetChanged();
    }

    protected boolean needCache() {
        return true;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final VH holder;
        final T t = mInfos.get(position);
        if (needCache()) {
            if (convertView == null) {
                convertView = View.inflate(mContext, getLayoutId(), null);
                holder = getHolder(convertView);
                if (holder == null) {
                    throw new NullPointerException("you must init Holder in your code");
                }
                beforeSetTag(convertView, position, parent, holder, t);
                convertView.setTag(holder);
            } else {
                holder = (VH) convertView.getTag();
            }
        } else {
            convertView =View.inflate(mContext,getLayoutId(),null);
            holder = getHolder(convertView);
        }
        bindView(position, holder, convertView, t);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(t, holder.itemView, position);
                }
            }
        });
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longClickListener != null) {
                    longClickListener.onItemLongClick(t, holder.itemView, position);
                    return true;
                } else {
                    return false;
                }

            }
        });
        return convertView;
    }

    protected void beforeSetTag(View convertView, int position, ViewGroup parent, VH holder, T bean) {

    }

    protected abstract void bindView(int position, VH holder, View convertView, T bean);

    protected abstract int getLayoutId();

    public abstract VH getHolder(View view);

    public List<T> getLists() {
        return mInfos;
    }

    public void setLists(List<T> infos) {
        this.mInfos.clear();
        this.mInfos.addAll(infos);
        notifyDataSetChanged();
    }

    public void ClearAndSetLists(List<T> infos) {
        this.mInfos = infos;
        notifyDataSetChanged();
    }

    public void remove(int position) {
        mInfos.remove(position);
        notifyDataSetChanged();
    }

    protected String setStrNoNull(String str) {
        if (str == null) {
            return "";
        } else {
            return str;
        }
    }

    public interface onItemClickListener<T> {
        void onItemClick(T bean, View itemView, int position);
    }

    public void setOnItemClickListener(onItemClickListener<T> listener) {
        this.listener = listener;
    }

    protected onItemClickListener listener;

    public interface onItemLongClickListener<T> {
        void onItemLongClick(T bean, View itemView, int position);
    }

    public void setOnItemLongClickListener(onItemLongClickListener<T> listener) {
        this.longClickListener = listener;
    }

    protected onItemLongClickListener longClickListener;

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }
}
